package com.sky.controller.user;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishItemVO;
import com.sky.vo.DishVO;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.impl.cookie.RFC2965DiscardAttributeHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController("userDishController")
@RequestMapping("/user/dish")
@Slf4j
@CrossOrigin
public class DishController {

    @Autowired
    private DishService dishService;

    @Autowired
    private RedisTemplate redisTemplate;


    @GetMapping("/list")
    @ApiOperation("通过分类id查询菜品")

    public Result<List<DishVO>> getByCategoryId(Long categoryId){
        log.info("通过分类id查询菜品：{}",categoryId);

        String key="dish_"+categoryId;

        List<DishVO> list = (List<DishVO>) redisTemplate.opsForValue().get(key);


        if(list!=null){
            //缓存中有数据
            return Result.success(list);
        }




        list=dishService.listWithFlavorEnabled(categoryId);
        redisTemplate.opsForValue().set(key, list);
        return Result.success(list);
    }


}
